package com.zhixue.media.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zhixue.media.model.po.MediaProcess;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author Dragon
 * @since 2023-09-25
 */
@Mapper
public interface MediaProcessMapper extends BaseMapper<MediaProcess> {


    /**
     * 根据分片参数获取待处理任务
     * @param shardTotal  分片总数
     * @param shardIndex  分片序号
     * @param count  任务数
     * @return
     */
    @Select("select * from media_process t where t.id % #{shardTotal} = #{shardIndex} and (t.status = '1' or t.status = '3') and t.fail_count < 3 limit #{count}")
    List<MediaProcess> selectListByShardIndex(@Param("shardTotal") int shardTotal, @Param("shardIndex") int shardIndex, @Param("count") int count);
    //QueryWrapper<MediaProcess> queryWrapper = new QueryWrapper<>();
    //    queryWrapper.apply("id % {0} = {1}", shardTotal, shardIndex)
    //                .and(wrapper -> wrapper.eq("status", "1").or().eq("status", "3"))
    //                .lt("fail_count", 3)
    //                .last("limit " + count);
    //    return mediaProcessMapper.selectList(queryWrapper);


    /**
     * 开启一个任务
     * @param id 任务id
     * @return 更新记录数
     */
    @Update("update media_process m set m.status='4' where (m.status='1' or m.status='3') and m.fail_count<3 and m.id=#{id}")
    int startTask(@Param("id") long id);


}
